package com.edu.test;

import com.edu.bean.Book;
import com.edu.mapper.BookMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @Author:mx
 * @Date:2025/10/20-17:04:11
 */
public class MyTest02 {

    SqlSession session1 = null;



    @Before
    public void init() throws Exception{
        String path = "mybatis-config.xml";
        InputStream ins = Resources.getResourceAsStream(path);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
        session1 = sqlSessionFactory.openSession();

    }


    @Test
    public void test02(){
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        PageHelper.startPage(2,10);
        List<Book> bookList = bookMapper.selectAll();
        Page<Book> page = (Page<Book>)bookList;
    }

    @Test
    public void test03() throws JsonProcessingException {
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        PageHelper.startPage(17,5);
        Page<Book> page = bookMapper.selectAll3();
        for (Book book : page) {
            System.out.println(book);
        }
//        PageInfo<Book> pageInfo = page.toPageInfo();//得到 分页相关的所有信息
        PageInfo<Book> pageInfo = new PageInfo<>(page,10);
        System.out.println("当前页的页码:"+pageInfo.getPageNum());
        System.out.println("页大小:"+pageInfo.getPageSize());
        System.out.println("总页数:"+pageInfo.getPages());
        System.out.println("当前页的数据数量:"+pageInfo.getSize());
        System.out.println("总条数:"+pageInfo.getTotal());
        System.out.println("有无下一页:"+pageInfo.isHasNextPage());
        System.out.println("有无上一页:"+pageInfo.isHasPreviousPage());
        System.out.println("分页条内部的数字：");
        for (int num : pageInfo.getNavigatepageNums()) {
            System.out.println(num);
        }
    }

}
